<template>
  <div class="mod-demo__equipment" style="overflow-y: scroll;height: 800px;" >
    <el-descriptions>
      <el-descriptions-item label="项目名称">{{routeParams.projectName}}</el-descriptions-item>
    </el-descriptions>
    <el-descriptions>
      <el-descriptions-item label="线路名称">{{routeParams.lineName}}</el-descriptions-item>
    </el-descriptions>
    <div style="height: 10px"></div>
    <el-form :inline="true" :model="state.dataForm" @keyup.enter="state.getDataList()">
      <el-form-item prop="projectId"><!---->
        <el-input style="display: none" v-model="state.dataForm.projectId" placeholder="" clearable></el-input>
      </el-form-item>
      <el-form-item label="设备名称">
        <el-input v-model="state.dataForm.name" placeholder="请输入设备名称" clearable></el-input>
      </el-form-item >
      <el-form-item label="所属线路">
        <el-select v-model="state.dataForm.lineId" placeholder="请选择线路"  clearable @change="selectLineOp">
          <el-option  v-for="data in view.lineList"   :key="data.id" :label="data.name" :value="data.id" ></el-option>
        </el-select>
      </el-form-item >
      <el-form-item label="设备类型">
        <el-select v-model="state.dataForm.equipmentTypeId" placeholder="请选择设备类型"  clearable>
          <el-option  v-for="data in view.typeList"  :label="data.name" :key="data.id" :value="data.id"></el-option>
        </el-select>
      </el-form-item >

      <el-form-item>
        <el-button @click="state.getDataList()">查询</el-button>
      </el-form-item>

      <el-form-item>
        <el-button  type="primary" @click="addOrUpdateHandle(routeParams)">新增设备</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="state.dataListLoading" :data="state.dataList" border @selection-change="state.dataListSelectionChangeHandle" style="width: 100%">
              <el-table-column prop="name" label="设备名称" header-align="center" align="center"></el-table-column>
              <el-table-column prop="projectName" label="所属项目" header-align="center" align="center"></el-table-column>
              <el-table-column prop="lineName" label="所属线路" header-align="center" align="center"></el-table-column>
              <el-table-column prop="lgt" label="经度" header-align="center" align="center"></el-table-column>
              <el-table-column prop="dms" label="维度" header-align="center" align="center"></el-table-column>
               <el-table-column type="index"  :index="indexMethod" label="排序" header-align="center" align="center"></el-table-column>
      <el-table-column prop="wgLgt" label="WGS84经度" header-align="center" align="center"></el-table-column>
      <el-table-column prop="wgDms" label="WGS84维度" header-align="center" align="center"></el-table-column>
              <el-table-column prop="source" label="定位点来源" header-align="center" align="center">
                <template v-slot="scope">
                  <el-tag v-if="scope.row.source === 1" size="small" type="error">RTK高精度定位</el-tag>
                  <el-tag v-if="scope.row.source === 2" size="small" type="error">差分定位</el-tag>
                  <el-tag v-if="scope.row.source === 3" size="small" type="error">单点定位</el-tag>
                  <el-tag v-if="scope.row.source === 4" size="small" type="error">GPS定位</el-tag>
                  <el-tag v-if="scope.row.source === 5" size="small" type="success">手动输入</el-tag>
                </template>
              </el-table-column>
              <el-table-column prop="equipmentName" label="设备名称" header-align="center" align="center"></el-table-column>
              <el-table-column label="图片">
                <template v-slot="scope">
                  <div class="block" v-for="item in scope.row.list" :key="fit">
                    <el-image
                      style="width: 50px; height: 50px"
                      :src="view.fileUrl+'?name='+ item.url"
                      fit="fill" @click="maxImg(item.url)"></el-image>
                  </div>
                </template>
              </el-table-column>
            <el-table-column label="操作" fixed="right" header-align="center" align="center" width="150">
        <template v-slot="scope">
         <span v-if="state.dataForm.lineId">
            <el-button  type="primary" link @click="upDataHandle(scope.row)">上移</el-button>
            <el-button  type="primary" link @click="downDataHandle(scope.row)">下移</el-button>
         </span>
          <el-button  type="primary" link @click="addOrUpdateHandle(scope.row)">修改</el-button>
          <el-button  type="primary" link @click="state.deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="state.page"
                   :page-sizes="[10, 20, 50, 100]" :page-size="state.limit"
                   :total="state.total" layout="total, sizes, prev, pager, next, jumper"
                   @size-change="state.pageSizeChangeHandle"
                   @current-change="state.pageCurrentChangeHandle"> </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update ref="addOrUpdateRef" @refreshDataList="state.getDataList">确定</add-or-update>
    <!-- 放大图片的对话框 -->
    <el-dialog  v-model="view.dialogVisible" >
      <img width="100%" :src="view.currentImage" alt="">
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import useView from "@/hooks/useView";
import { reactive, ref, toRefs,watch } from "vue";
import AddOrUpdate from "./equipment-add-or-update.vue";
import {useRoute} from "vue-router";
import baseService from "@/service/baseService";
import app from "@/constants/app";
import {ElMessage, ElMessageBox} from "element-plus";
import {IObject} from "@/types/interface";
const route = useRoute();
const routeParams = ref({});
routeParams.value = route.params;
const view = reactive({
  deleteIsBatch: true,
  getDataListURL: "/sysproject/equipment/page",
  getDataListIsPage: true,
  exportURL: "/sysproject/equipment/export",
  deleteURL: "/sysproject/equipment",
  dataForm:{projectId:"",lineId:""},
  typeList:[],
  lineList:[],
  fileUrl : `${app.api}/sys/oss/downloadLocal`,
  dialogVisible:false,
  currentImage:""
});
const maxImg = (url?:string) => {
  view.dialogVisible=true;
  view.currentImage =view.fileUrl+'?name='+url
}
const indexMethod = (val:number) => {
  let num =state.page;
  num =num-1
 return val+1+(state.limit * num);
}
const selectLineOp = () => {
  debugger;
  state.query();
}
const state = reactive({ ...useView(view), ...toRefs(view) });
const addOrUpdateRef = ref();
const addOrUpdateHandle = (row?: {}) => {
  addOrUpdateRef.value.init(row);
};
// 获取信息
baseService.get("sysapp/equipmenttype/getType").then((res) => {
  Object.assign(view.typeList, res.data);
});
routeParams.value = route.params;
const upDataHandle =(row:{})=>{
  row.type = 2
  ElMessageBox.confirm("确定进行[上移]操作?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(() => {
    (!row.id ? baseService.post : baseService.put)("/sysproject/equipment", row).then((res) => {
      ElMessage.success({
        message: '成功',
        duration: 500,
        onClose: () => {
          state.query();
        }
      });
    });
    }).catch(() => {
    });
}
const downDataHandle =(row:{})=>{
  row.type = 1
  ElMessageBox.confirm("确定进行[上移]操作?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(() => {
      (!row.id ? baseService.post : baseService.put)("/sysproject/equipment", row).then((res) => {
        ElMessage.success({
          message: '成功',
          duration: 500,
          onClose: () => {
           state.query();
          }
        });
      });

    }).catch(() => {
    });
}

if(state.dataForm.projectId == ""){
  if(null !=routeParams.value.projectId){
    console.log("项目进来的")
    state.dataForm.projectId = routeParams.value.projectId;
    state.getDataList();
  }
}
if(state.dataForm.lineId == ""){
  debugger
  if(null !=routeParams.value.lineId){
    console.log("线路进来的")
    state.dataForm.projectId = routeParams.value.projectId;
    state.dataForm.lineId = routeParams.value.lineId;
    state.getDataList();
  }
}

watch(() => route.params?route.params:undefined, (newId) => {
  debugger
  console.log("开始监视路由变化")
  console.log()
  if(Object.keys(newId).length !== 0){
    routeParams.value = newId;
    state.dataForm.projectId = routeParams.value.projectId;
    state.dataForm.lineId = routeParams.value.lineId;
    getLineByProjectId();
    state.getDataList();

    /*if(routeParams.value.projectId != newId.projectId){
      routeParams.value = newId;
      state.dataForm.projectId = routeParams.value.projectId;
      state.dataForm.lineId = routeParams.value.lineId;
      getLineByProjectId();
      state.getDataList();
    }else if(routeParams.value.lineId != newId.lineId){

    }*/
  }
})
// 获取信息
const getLineByProjectId=()=>{
  baseService.get("sysproject/line/getLineByProjectId"+"?projectId="+route.params.projectId).then((res) => {
    Object.assign(view.lineList, res.data);
  });
}
getLineByProjectId();


</script>
